package com.cloud.bssp.juc.reetrantlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description： 定时测试
 * @author：weirx
 * @date：2021/12/13 15:46
 * @version：3.0
 */
public class TryLockTest {

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();

        Thread t1 = new Thread(() -> {
            if (!lock.tryLock()) {
                System.out.println("获取锁失败");
                return;
            } else {
                try {
                    System.out.println("获取锁成功");
                } finally {
                    lock.unlock();
                }
            }

        });

        lock.lock();
        try {
            t1.start();
            TimeUnit.SECONDS.sleep(1);
        } finally {
            lock.unlock();
        }
    }
}
